/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.map;

import com.google.common.collect.ImmutableList;
import genomemap.data.KSA00Data;
import genomemap.map.ContigMap.Contig;

/**
 * A data object
 *
 * @author Susanta Tewari
 * @version 1.0 Apr 9, 2010
 */
public final class ProbeMap extends GenomeMap {

    /**
     * estimates spacings between probes
     */
    private final KSA00Data data;

    /**
     * Constructs ...
     *
     *
     * @param data
     * @param title
     */
    public ProbeMap(final KSA00Data data, final String title) {

        super(data.getModel().getProbes(), title);

        this.data = data;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public KSA00Data getMapData() {
        return data;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public ImmutableList<String> getProbes() {
        return getLabels();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public Double estimateChromosomeSize() {

        double result = 0;

        for (final Double spacing : data.getModel().getProbeSpacing()) {
            result += spacing;
        }

        result += getProbes().size() * data.getModel().getCloneLength();

        return result;
    }

    /**
     * Method description
     *
     *
     * @param contig
     *
     * @return
     */
    public Double estimateContigSize(final Contig contig) {

        double result                             = 0;
        final ImmutableList<String> probes        = getProbes();
        final int startIndex                      = probes.indexOf(contig.getStartProbe());
        final int endIndex                        = probes.indexOf(contig.getEndProbe());
        final ImmutableList<Double> probeSpacings = data.getModel().getProbeSpacing();

        for (int i = startIndex; i <= endIndex; i++) {
            result += probeSpacings.get(i);
        }

        result += contig.getProbes().size() * data.getModel().getCloneLength();

        return result;
    }

    /**
     * The gap between this probe and the next one. If this is the last one, then throws
     * exception.
     *
     * @param probe a probe in this map
     * @return gap between the specified probe and the next one
     */
    public Double getProbeGap(final String probe) {

        final ImmutableList<String> probes = getProbes();
        final int probeIndex               = probes.indexOf(probe);

        if (probeIndex == -1) {
            throw new IllegalArgumentException("probe not found");
        } else if (probeIndex == probes.size() - 1) {
            throw new IllegalArgumentException("probe is the last on the map");
        }

        return data.getModel().getProbeSpacing().get(probeIndex);
    }
}
